import axios from 'axios'

// import {Loading} from 'element-ui'
// var loadingInstance;
var timeout;

// import showAlert from '@/common/js/errorHandle'

//login session 失效
// import showLogin from '@/common/js/loginPop'
 

export default{
//	 baseURI: window.location.origin,
    loadingInstance:null,
    baseURI: "http://10.5.0.137:8080",
	Post : function(url, params, method){

		//发送ajax请求
		return axios.request({
			method: method || 'POST',
			url : url,
			//params : params,微服务参数放在了url后
			// params:params,
			baseURL: this.baseURI,
			// params:params,
			
			timeout: 15000,
			responseType: 'json',
			headers : {
                "Content-Type": "application/json"
			},
            data:params,
			transformRequest : function(data){
                // let ret = ''
		        // for (let it in data) {
		        //   ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
		        // }
                // let loadingInstance = Loading.service({fullscreen:HTMLOptGroupElement});
                // loadingInstance.close();
                // if (!loadingInstance) {
                //     timeout = setTimeout(function(){
                //         loadingInstance && loadingInstance.close();
                //         showAlert.showAlert("网络出错,请检查网路是否连接");
                //     },15000)
                //     loadingInstance = Loading.service({
                //         target:'div.content', 
                //         text:'加载中',
                //         customclass:'load-mask'
                //     });
             
                // }
                return JSON.stringify(data);
			},
			transformResponse : function(response){
				// 这里提前处理返回的数据
                console.log(response,"response");
				// if(response&&response.status&&response.status==='01'){
				// 	window.localStorage.removeItem('hyoaTK');
				// 	window.location.href='/module/login.html';
				// }
                typeof(response)==='string' && (response = JSON.parse(response)) 
                
                // if (response.status !== 1 && response.status !== 200) {
                //     var message = response.message;
                //     showAlert.showAlert(message);
                //     if (message.indexOf('session') !== -1) {
                //         showLogin.showLoginPanel(message);
                //     } 
                   
                // }

                // window.clearTimeout(timeout);
                // timeout = null;
                // loadingInstance && loadingInstance.close();
                // loadingInstance = null;
				return response;
			},
			validateStatus: function (state) {
				//请求返回状态
                console.log(state,"state");
    			return state >= 200 && state < 300;
			},
            withCredentials: true
		});
	},
  
    //以FormData形式发送POST请求，可用来上传文件
    axiosPostFormData : function(url,form){
        //发送ajax请求
        return axios.post(
        url,
        form,
        {
            method: 'post',
            baseURL: this.baseURI,
            headers: {"Content-Type": "multipart/form-data"},
            transformRequest : function(data){
            // 这里可以在发送请求之前对请求数据做处理，比如form-data格式化等，这里可以使用开头引入的Qs（这个模块在安装axios的时候就已经安装了，不需要另外安装）
            if(data && data instanceof FormData){
            }else{
                let formdata = new FormData();
                let i = 0;
                for (let it in data) {
                formdata.append(it, data[it]);
                }
                return formdata;
            }
            return data;
            },
            transformResponse : function(response){
            // 这里提前处理返回的数据
            if(response&&response.status&&response.status==='01'){
                window.localStorage.removeItem('hyoaTK');
                window.location.href='/module/login.html';
            }
            var res;
            if(typeof(response)==='string'){
                res=JSON.parse(response);
            }else{
                res=response;
            }
            var result = {
                status : status,
                data : res
            };
            return result;
            }
        }
        );
    }
}
